package com.ruoyi.common.utils.poi;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Range;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Iterator;
import java.util.Map;

public class DocUtils {
    public static void writeWord(Map<String,String> map,String filePath,String targetFileName,String generateFileName){
        //读取word模板
//        String fileDir = new File(base.getFile(),"http://www.cnblogs.com/http://www.cnblogs.com/../doc/").getCanonicalPath();
        FileInputStream in = null;
        try {
            in = new FileInputStream(new File(filePath+targetFileName));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        HWPFDocument hdt = null;
        try {
            hdt = new HWPFDocument(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Fields fields = hdt.getFields();
        Iterator<Field> it = fields.getFields(FieldsDocumentPart.MAIN).iterator();

        //读取word文本内容
        Range range = hdt.getRange();
        //替换文本内容
        for (Map.Entry<String,String> entry: map.entrySet()) {
            if(entry.getValue() == null){
                range.replaceText("$" + entry.getKey() + "$","");
            }else{
                range.replaceText("$" + entry.getKey() + "$",entry.getValue());
            }

        }
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath+generateFileName,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            hdt.write(ostream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //输出字节流
        try {
            out.write(ostream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            ostream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downFile(String url, String fileName, HttpServletResponse response){
        try {
            File file = new File(url);
            response.reset();
            response.setContentType("bin");
            response.setHeader("Content-Disposition","attachment; filename=\"" + new String(fileName.getBytes("GBK"), "ISO8859_1") + "\"");
            OutputStream out = response.getOutputStream();
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            fis.read(data);
            fis.close();
            out.write(data);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
